function commafy (num) {
  let symbol = ''
  let intPart = num + ''
  let floatPart = ''
  num = num + ''

  if (num.indexOf('-') > -1) { // 是否包含负号
    symbol = '-'
    num = num.substring(1)
    intPart = num
  }

  if (intPart.indexOf('.') > 0) { // 是否包含小数部分
    let numPart = intPart.split('.')
    intPart = numPart[0]
    floatPart = numPart[1]
  }

  let intPartArr = intPart.split('').reverse()
  let intPartNewArr = [] // 处理后的字符串
  intPartArr.forEach((num, index) => {
    intPartNewArr.push(num)
    if (((index + 1 ) % 3 === 0) &&(index !== (intPartArr.length -1))) { // 第三个位置且不是最后一位
      intPartNewArr.push(',')
    }
  })

  initPart = intPartNewArr.reverse().join('')
  floatPart = floatPart === '' ? '': ('.' + floatPart)
  return symbol + initPart + floatPart
}

console.log(commafy(12000000.11))
console.log(commafy(-100000.33))
console.log(commafy(0.11))
console.log(commafy(12345))
console.log(commafy(11))
